<template>
    <div id="login" class="login_bg">
        <common-login :commonData="{$parent:$parent}"></common-login>
        <div class="login_box">
            <ul class="text_center login_box_ul">
                <li>
                    <router-link class="login_tab_active" to="/login">个人登录</router-link>
                </li>
                <li>
                    <router-link to="/company_login">企业登录</router-link>
                </li>
            </ul>
            <form class="login_form" ref="form" :model="form" @keyup.enter="toLogin(form)">
                <div class="login_input_box">
                    <img class="login_icon" src="../../static/img/login/login_user.png">
                    <input style="color: #333333;" type="text" v-model.trim="form.telephone" placeholder="请输入手机号或身份证号">
                </div>
                <div class="login_input_box">
                    <img class="login_icon" src="../../static/img/login/login_pwd.png">
                    <input type="password" v-model.trim="form.pwd" placeholder="请输入您的密码" style="color: #333333;">
                </div>
                <div class="login_input_box login_input_code">
                    <img class="login_icon" src="../../static/img/login/login_code.png">
                    <input type="text" style="color: #333333;" v-model.trim="form.code" placeholder="验证码">
                    <span class="login_code" id="loginCode">
                        <img :src="'data:image/png;base64,'+base64Code" @click="clickCodeImg" width="150px"
                            height="60px">
                    </span>
                </div>
                <p
                    style="color: #666666;text-align: center;margin-top: 30px;margin-bottom: -30px;color: #ff0000;font-size: 15px;">
                    默认账号：身份证号，密码：身份证后6位！</p>
                <div class="login_btn_box text_center">
                    <a class="login_btn" @click="toLogin(form)">立即登录</a>
                    <router-link class="login_a" to="/forget_pwd">忘记密码</router-link>
                    <span>|</span>
                    <router-link class="login_a" to="/register">点击注册</router-link>
                </div>
            </form>
        </div>
    </div>
</template>

<script>
    import commonLogin from '../components/c_login.vue'
    export default {
        name: 'login',
        data() {
            return {
                base64Code: "",

                form: {
                    telephone: "",
                    pwd: "",
                    code: "",
                    v_token: ""
                }
            }
        },
        beforeCreate() {
            this.$cookies.set("loginInfo", undefined);
            this.$cookies.set("token", undefined);
        },
        created: function() {
            this.getImgCode();
        },
        methods: {
            clickCodeImg: function() {
                this.getImgCode();
            },
            toLogin: function(dataForm) {
                var that = this;
                // if (dataForm.telephone == "") {
                //     this.$message.error('请输入手机号或身份证号！');
                //     return;
                // }
                // if (dataForm.pwd == "") {
                //     this.$message.error('请输入密码！');
                //     return;
                // }
                // if (dataForm.code.length != 4) {
                //     this.$message.error('请输入正确的验证码！');
                //     return;
                // }
                // this.common.post(this.api.login, dataForm, function(data) {
                //     data.resultMap.userType = 0;
                //     console.log("打印data.resultMap",data.resultMap);
                //     that.$cookies.set("loginInfo", data.resultMap, that.api.config
                //     .tokenTime); // 设置cookie中存放的生命周期
                //     that.$cookies.set("token", data.resultMap.token, that.api.config
                //     .tokenTime); // 设置cookie中存放的生命周期
                //     that.$parent.loginInfo = data.resultMap;
                //     that.$message.success("恭喜您登陆成功！");

                //     that.common.toPage('/index', true);
                // }, function(error) {
                //     that.getImgCode();
                //     that.$message.error(error.resultMsg);
                // });

                // console.log(dataForm);
                // console.log(this.api.login);
                // console.log(this.$sdk);
                this.$sdk.$request.post(this.api.login,dataForm).then(data=>{
                    console.log(data);
                     data.resultMap.userType = 0;
                    console.log("打印data.resultMap --就是cookie信息",data.resultMap);
                    this.$cookies.set("loginInfo", data.resultMap, this.api.config
                    .tokenTime); // 设置cookie中存放的生命周期
                    this.$cookies.set("token", data.resultMap.token, this.api.config
                    .tokenTime); // 设置cookie中存放的生命周期
                    this.$parent.loginInfo = data.resultMap;
                    this.$message.success("恭喜您登陆成功！");

                    this.common.toPage('/index', true);
                }).catch(err=>{
                this.$message.error(err.resultMsg);
            });
            },
            getImgCode: function() {
                // console.log("执行二维码");
                console.log("查看请求地址",this.api.verifyCode)
                this.$sdk.$request.post(this.api.verifyCode,{}).then(res=>{
                    console.log(res);
                    this.base64Code = res.base64;
                    this.form.v_token = res.v_token;
                    console.log('结束');
                })
                // var that = this;
                // this.common.post(this.api.verifyCode, {}, function(data) {
                //     that.base64Code = data.base64;
                //     that.form.v_token = data.v_token;
                // }, function(error) {
                //     that.$message.error(error.resultMsg);
                // });
            }
        },
        components: {
            "common-login": commonLogin
        }
    }
</script>
<style>
    @import url("../assets/css/login.css");
</style>
